<template>
  <div class="page-modal">
    <el-dialog
      v-model="dialogVisible"
      destroy-on-close
      title="新建权限"
      width="30%"
      center
    >
      <MyForm v-bind="modalConfig" v-model="formData"></MyForm>
      <slot></slot>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmClick"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 分配权限的弹窗 -->
    <el-dialog
      v-model="permDialogVisible"
      destroy-on-close
      title="分配权限"
      width="30%"
      center
    >
      <el-tree
        :data="permTree"
        :props="treeProps"
        show-checkbox
        node-key="name"
        default-expand-all
        :default-checked-keys="rolePerm"
        @check="handleCheck"
      />
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="permDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="setPerm"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch, computed } from "vue"
import MyForm from "@/base-ui/form"
import { systemStore } from "@/stores/modules/system"
import { mainStore } from "@/stores/modules/main"
import { PermRole } from "@/service/api"
import { ElMessage } from "element-plus"

export default defineComponent({
  props: {
    modalConfig: {
      type: Object,
      required: true
    },
    defaultInfo: {
      type: Object,
      default: () => ({})
    },
    pageName: {
      type: String,
      required: true
    }
  },
  components: {
    MyForm
  },
  setup(props) {
    const dialogVisible = ref(false)
    const permDialogVisible = ref(false)
    const formData = ref<any>({})
    const systemstore = systemStore()
    const mainstore = mainStore()
    // 权限树数据
    const permTree = computed(() => mainstore.permTree)
    // 角色权限
    const rolePerm = computed(() => mainstore.rolePerm)
    const treeProps = {
      label: "label",
      children: "children"
    }
    watch(
      () => props.defaultInfo,
      (newValue: any) => {
        for (const item of props.modalConfig.formItems) {
          formData.value[`${item.field}`] = newValue[`${item.field}`]
        }
      }
    )
    // 点击确定
    const confirmClick = () => {
      dialogVisible.value = false
      if (Object.keys(props.defaultInfo).length) {
        // 编辑
        systemstore.editPageInfo({
          url: "/" + props.pageName + "/",
          id: props.defaultInfo.id,
          newData: { ...formData.value }
        })
      } else {
        // 新建
        systemstore.addPageInfo({
          url: "/" + props.pageName,
          newData: { ...formData.value }
        })
      }
    }
    const treeInfo = ref<any>()
    const setId = ref<number>()
    // 选择框选中
    const handleCheck = (data1: any, data2: any) => {
      const checkedKeys = data2.checkedKeys
      const halfCheckedKeys = data2.halfCheckedKeys
      // console.log(checkedKeys)
      // console.log(halfCheckedKeys)
      const tree = [...checkedKeys, ...halfCheckedKeys]
      treeInfo.value = { tree }
    }
    // 分配权限
    const AssignPermissions = async (id: number) => {
      // 获取角色权限
      await mainstore.getOptionRole(id)
      permDialogVisible.value = true
    }
    // 确定分配权限
    const setPerm = async () => {
      console.log(setId.value, treeInfo.value)
      const res = await PermRole(setId.value as number, { ...treeInfo.value })
      ElMessage.success(res.message)
      permDialogVisible.value = false
    }
    return {
      dialogVisible,
      permDialogVisible,
      formData,
      treeProps,
      mainstore,
      permTree,
      rolePerm,
      confirmClick,
      AssignPermissions,
      setPerm,
      setId,
      handleCheck
    }
  }
})
</script>

<style scoped lang="less"></style>
